package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.dao.cargo.ShippingOrderDao;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.service.cargo.ShippingOrderService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ShippingOrderServiceImpl implements ShippingOrderService {

    @Autowired
    private ShippingOrderDao shippingOrderDao;
    @Autowired
    private ExportDao exportDao;
    @Autowired
    private PackingDao packingDao;

    @Override
    public PageInfo findAll(String id, int pageNum, int size) {
        PageHelper.startPage(pageNum, size);
        List<ShippingOrder> list = shippingOrderDao.findByCompanyId(id);
        return new PageInfo(list);
    }

    @Override
    public void save(ShippingOrder shippingOrder) {
        //查询报运单  进行数据封装   修改装箱单数据  不在添加页面显示
        Export export = exportDao.selectByPrimaryKey(shippingOrder.getShippingOrderId());
        if (export != null) {
//            修改装箱单数据  不在添加页面显示
            Packing packing = packingDao.selectByPrimaryKey(shippingOrder.getShippingOrderId());
            packing.setState(2L);//显示已委托
            packingDao.updateByPrimaryKeySelective(packing);
            //数据封装
            //装箱港
            shippingOrder.setPortOfLoading(export.getShipmentPort());
            //卸货港
            shippingOrder.setPortOfDischarge(export.getDestinationPort());
            //运输方式
            shippingOrder.setOrderType(export.getTransportMode());
//        save;
            shippingOrderDao.insert(shippingOrder);
        }
    }

    @Override
    public ShippingOrder findById(String id) {
        ShippingOrder packing = shippingOrderDao.selectByPrimaryKey(id);
        return packing;
    }

    @Override
    public void update(ShippingOrder packing) {
        shippingOrderDao.updateByPrimaryKeySelective(packing);
    }

    @Override
    public void deleteById(String id) {
        //删除
        shippingOrderDao.deleteByPrimaryKey(id);
        //修改装箱单的状态  变为已上报
        Packing packing = packingDao.selectByPrimaryKey(id);
        packing.setState(1L);
        packingDao.updateByPrimaryKeySelective(packing);
    }

    @Override
    public void cancel(String id) {
        ShippingOrder shippingOrder = shippingOrderDao.selectByPrimaryKey(id);
        if (shippingOrder.getState() == 1) {
            //上报未发票
            shippingOrderDao.alterState(id, 0);
        }
    }

    @Override
    public void submit(String id) {
        //提交委托单
        //修改委托单状态  0草稿  1提交     修改报运单的状态
        ShippingOrder shippingOrder = shippingOrderDao.selectByPrimaryKey(id);
        if (shippingOrder.getState() == 0) {
            //草稿
            shippingOrderDao.alterState(id, 1);
            Export export = exportDao.selectByPrimaryKey(id);
            export.setState(3);
            exportDao.updateByPrimaryKeySelective(export);
        }
    }

}
